//
//  FamiliarityNumberViewController.m
//  NuwaRobot
//
//  Created by Kendama on 2017/12/4.
//  Copyright © 2017年 Kendama. All rights reserved.
//

#import "FamiliarityNumberViewController.h"
#import "AddFamiliarityNumberViewController.h"
#import "MyInfoTableViewCell.h"
#import "GetAffectionNumberListApi.h"
#import "DeleteAffectionNumberApi.h"
@interface FamiliarityNumberViewController ()<UITableViewDelegate,UITableViewDataSource,MGSwipeTableCellDelegate>

@property(nonatomic,strong) UITableView *tableView;
@property(nonatomic,strong) NSMutableArray *dataSource;

@end

@implementation FamiliarityNumberViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self getAffectionNumberList];
}

#pragma mark --- 点击事件
- (void)onRightButtonClick:(id)sender
{
    AddFamiliarityNumberViewController *vc = [AddFamiliarityNumberViewController new];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark --- 事件处理
- (void)getAffectionNumberList
{
    GetAffectionNumberListApi *api = [[GetAffectionNumberListApi alloc] initWithDic:@{@"sn":EXRACT_OBJECT(DEVICEMAC)}];
    [api dtStartYTKRequestCompletionBlockWithSuccess:^(YTKBaseRequest * _Nonnull request) {
        NSDictionary *dict = [request responseJSONObject];
        [self.dataSource removeAllObjects];
        [self.dataSource addObjectsFromArray:dict[@"data"]];
        [self.tableView reloadData];
    } failure:nil];
}

#pragma mark --- tabelview代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataSource.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return px1920Height(150);
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return px1920Height(30);
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyInfoTableViewCell *cell = [MyInfoTableViewCell dequeueReusableWithTableView:tableView];
    NSDictionary *data = self.dataSource[indexPath.section];
    cell.titleLabel.text = data[@"fullname"];
    cell.infoLabel.text = data[@"mobile"];
    cell.rightImageView.hidden = YES;
    cell.rightButtons = @[[MGSwipeButton buttonWithTitle:@"删除" backgroundColor:rgba(251,74,79,1)]];
    cell.delegate = self;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

-(BOOL) swipeTableCell:(UITableViewCell *) cell tappedButtonAtIndex:(NSInteger) index direction:(MGSwipeDirection)direction fromExpansion:(BOOL) fromExpansion
{
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    if (direction == MGSwipeDirectionRightToLeft)
    {
        switch (index) {
            case 0:
            {
                [[AlertViewManager shareManager] initWithTitle:@"提示" message:@"您确定要删除此成员？" cancleButtonTitle:@"取消" OtherButtonsArray:@[@"确定"] clickAtIndex:^(NSInteger buttonIndex, UIAlertController *alertController) {
                    if (buttonIndex == 1)
                    {
                        DeleteAffectionNumberApi *api = [[DeleteAffectionNumberApi alloc] initWithDic:@{@"id":self.dataSource[indexPath.section][@"id"],@"device_id":EXRACT_OBJECT(DEVICEID)}];
                        [api dtDefaultInterceptStartYTKRequestCompletionBlockWithSuccess:^(YTKBaseRequest * _Nonnull request) {
                            NSDictionary *dic = [request responseJSONObject];
                            if ([dic[@"errcode"] intValue] == 0)
                            {
                                [SVProgressHUD showSuccessWithStatus:@"删除成功"];
                                [self getAffectionNumberList];
                            }
                            else
                            {
                                [SVProgressHUD showSuccessWithStatus:dic[@"errmsg"]];
                            }
                        } failure:nil];
                    }
                }];
            }
                break;
                
                
            default:
                break;
        }
        
    }
    
    return YES;
}

#pragma mark --- 懒加载
- (UITableView *)tableView
{
    if(!_tableView)
    {
        UIView *superView = self.view;
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStyleGrouped];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        [superView addSubview:_tableView];
        _tableView.backgroundColor = [UIColor clearColor];
    }
    return _tableView;
}

- (NSMutableArray *)dataSource
{
    if(!_dataSource)
    {
        _dataSource = [[NSMutableArray alloc]init];
    }
    return _dataSource;
}


- (void)masLayoutSubview
{
    __weak UIView *superView = self.view;
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(superView.mas_top).with.offset(64);
        make.left.mas_equalTo(superView.mas_left).with.offset(px1080Width(40));
        make.right.mas_equalTo(superView.mas_right).with.offset(-px1080Width(40));
        make.bottom.mas_equalTo(superView.mas_bottom);
    }];
    
    
}

- (void)setNavigationStyle
{
    [self setNavigationBarTitle:@"亲情号码设置"];
    [self setRightButtonImage:imageWithName(@"nav_add")];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
